package com.simtop.picture.controller;

import com.simtop.picture.common.ServerResponse;
import com.simtop.picture.pojo.Role;
import com.simtop.picture.pojo.User;
import com.simtop.picture.service.UserService;
import com.simtop.picture.utils.EmptyUtil;
import com.simtop.picture.utils.JwtUtil;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/user")
@CrossOrigin //todo 跨域问题
public class UserController {


    @Autowired
    public UserService userService;

    private static Logger logger= LoggerFactory.getLogger(UserController.class);

    /**
     * 获取角色身份
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getRole",method = RequestMethod.GET)
    public ServerResponse<List<Role>> getRole(){
        List<Role> roleList=userService.getRole();
        return ServerResponse.createBySuccess(roleList);
    }

    /**
     * 用户登录（返回类）
     * @param user（loginName，password，roleId）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ServerResponse<String> login(@RequestBody User user){
        logger.info("登录"+user);
        User loginUser =userService.login(user);
        if(EmptyUtil.isNotEmpty(loginUser)){
//            loginUser.setCreateTime(new Date());
            loginUser.setLoginTime(System.currentTimeMillis());
            loginUser.setPassword(org.apache.commons.lang3.StringUtils.EMPTY);//置空密码
            // todo important token有效期10个小时
            String token = JwtUtil.sign(loginUser,600L*1000L*6L);//token有效期10小时
            return ServerResponse.createBySuccess(token);
        }else{
            return ServerResponse.createByErrorMsg("账号名或者密码输入错误");
        }
    }


    /**
     * 注册用户
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register",method =RequestMethod.POST)
    public ServerResponse<String> register (@RequestBody User user){
        logger.info("注册用户"+user);
        return userService.register(user);
    }


    /**
     * 注册时 1、获取后台生成的邮箱验证码
     * 参数需要安全，接口安全
     * @return
     */
    @RequestMapping(value = "/checkCode",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> sendEmailCode(@RequestBody String email){
        JSONObject jsonObj = new JSONObject(email);
        logger.info("email"+jsonObj.getString("email"));
        //根据邮箱获取验证码
        return userService.generateCheckCode(jsonObj.getString("email"));
    }

    /**
     * 忘记密码时获取验证码 邮箱验证码
     *
     */
    @RequestMapping(value = "/forgetCheckCode",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetSendEmailCode(@RequestBody String email){
        JSONObject jsonObj = new JSONObject(email);
        logger.info("email"+jsonObj.getString("email"));
        //根据邮箱获取验证码
        return userService.forgetSendEmailCode(jsonObj.getString("email"));
    }

    /**
     * 忘记密码时根据邮箱地址获取验证码 更新
     */
    @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updatePassword(@RequestBody User user){
        logger.info("更新用户"+user);
        return userService.updatePassword(user);
    }

}
